/*
 * @Description: 
 * @Version: 0.1
 * @Autor: 许振富 zfgj201511
 * @Date: 2025-01-18 15:30:23
 * @FilePath: \stm\System\hlk_protocol\zw0906_cmd.c
 * @LastEditors: 许振富(jia)
 * @LastEditTime: 2025-01-18 15:41:02
 */
#include "zw0906_protocol.h"
#include "zw0906_cmd.h"
#include "ASM_Delay.h"

extern void usart1_send(unsigned char _data);

unsigned char zw0906_ps_getimage(unsigned char* _st)
{
    unsigned char temp[32] = {0},st = 0;
    unsigned short t_len = 0, each = 0;
    zw0906_protocol_Data_Packaging(0x01, 0x01, (void *)0, 0, temp, &t_len);
    for (each = 0; each < t_len; each++)
    {
        usart1_send(temp[each]);
    }
    each = 0;
    do
    {
        if(zw0906_p.Flag == zw0906_Valid)
        {
            break;
        }
        ASM_DELAY_MS(1);
        each ++;
    } while (each < 500);
    if(zw0906_p.Flag == zw0906_Valid)
    {
        st = 1;
		*_st = zw0906_p.Rx_Buffer.enter;
		zw0906_protocol_Reset(&zw0906_p);
    }
    return st;
}

unsigned char zw0906_ps_genchar(unsigned char* _st)
{
    unsigned char temp[32] = {0},st = 0;
    unsigned short t_len = 0, each = 0;
    zw0906_protocol_Data_Packaging(0x01, 0x02, (void *)"\x01", 1, temp, &t_len);
    for (each = 0; each < t_len; each++)
    {
        usart1_send(temp[each]);
    }
    each = 0;
    do
    {
        if(zw0906_p.Flag == zw0906_Valid)
        {
            break;
        }
        ASM_DELAY_MS(1);
        each ++;
    } while (each < 500);
    if(zw0906_p.Flag == zw0906_Valid)
    {
        st = 1;
		*_st = zw0906_p.Rx_Buffer.enter;
		zw0906_protocol_Reset(&zw0906_p);
    }
    return st;
}

unsigned char zw0906_ps_search(unsigned char* _st)
{
    unsigned char temp[32] = {0},st = 0;
    unsigned short t_len = 0, each = 0;
    zw0906_protocol_Data_Packaging(0x01, 0x04, (void *)"\x01\x00\x00\x00\x64", 5, temp, &t_len);
    for (each = 0; each < t_len; each++)
    {
        usart1_send(temp[each]);
    }
    each = 0;
    do
    {
        if(zw0906_p.Flag == zw0906_Valid)
        {
            break;
        }
        ASM_DELAY_MS(1);
        each ++;
    } while (each < 500);
    if(zw0906_p.Flag == zw0906_Valid)
    {
        st = 1;
		_st[0] = zw0906_p.Rx_Buffer.enter;
		_st[1] = zw0906_p.Rx_Buffer.Data[0];
		_st[2] = zw0906_p.Rx_Buffer.Data[1];
		_st[3] = zw0906_p.Rx_Buffer.Data[2];
		_st[4] = zw0906_p.Rx_Buffer.Data[3];
		zw0906_protocol_Reset(&zw0906_p);
    }
    return st;
}

unsigned char zw0906_ps_controlbln(unsigned char* _st,unsigned char _len_status)
{
    unsigned char temp[32] = {0},st = 0;
    unsigned short t_len = 0, each = 0;
	unsigned char led_on_b[4] = "\x03\x03\x03\x00";
	unsigned char led_off[4] = "\x04\x03\x03\x00";
	unsigned char led_on_r[4] = "\x03\x04\x03\x00";
	unsigned char led_on_g[4] = "\x03\x02\x03\x00";
	unsigned char* cmd;

	switch(_len_status)
	{
		case(2):cmd = led_on_r; break;
		case(3):cmd = led_on_g; break;
		case(4):cmd = led_on_b; break;
		case(5):cmd = led_off; break;
		default:cmd = led_off; break;
	}
	
    zw0906_protocol_Data_Packaging(0x01, 0x3C, cmd ,4, temp, &t_len);
    for (each = 0; each < t_len; each++)
    {
        usart1_send(temp[each]);
    }
    each = 0;
    do
    {
        if(zw0906_p.Flag == zw0906_Valid)
        {
            break;
        }
        ASM_DELAY_MS(1);
        each ++;
    } while (each < 500);
    if(zw0906_p.Flag == zw0906_Valid)
    {
        st = 1;
		*_st = zw0906_p.Rx_Buffer.enter;
		zw0906_protocol_Reset(&zw0906_p);
    }
    return st;
}

unsigned char zw0906_check(void)
{
	uint8_t temp[32] = {0xFF},st = 0;
	uint16_t return_sta = 0; //返回状态
	uint16_t return_len = 0; //返回长度

	return_len = 1;
// 	if(temp[0] == 0)
// 	{
		temp[0] = 0xFF;
		zw0906_ps_getimage(&temp[0]);
// 	}
	if(temp[0] == 0)
	{
		temp[0] = 0xFF;
		zw0906_ps_genchar(&temp[0]);
	}
	if(temp[0] == 0)
	{
		temp[0] = 0xFF;
		zw0906_ps_search(&temp[0]);
	}
// 	if(temp[0] == 0)
// 	{
// 		return_len = 5;
// 	}
	st = temp[0];
	return st;
}
